package com.sxhalo.manage.web.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.BeanUtils;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sxhalo.manage.map.Department;
import com.sxhalo.manage.service.IBaseService;
import com.sxhalo.manage.utils.Pager;
import com.sxhalo.manage.web.form.DepartmentForm;

public class DepartmentAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private int draw = 1;

	private int start = 0;

	private int length = 20;

	private DepartmentForm department;

	public DepartmentForm getDepartment() {
		return department;
	}

	public void setDepartment(DepartmentForm department) {
		this.department = department;
	}

	private Map<String, Object> dataMap;

	private List dataList;

	public int getDraw() {
		return draw;
	}

	public void setDraw(int draw) {
		this.draw = draw;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public List getDataList() {
		return dataList;
	}

	public void setDataList(List dataList) {
		this.dataList = dataList;
	}

	public int getLength() {
		return length;
	}

	public void setLength(int length) {
		this.length = length;
	}

	public Map<String, Object> getDataMap() {
		return dataMap;
	}

	public void setDataMap(Map<String, Object> dataMap) {
		this.dataMap = dataMap;
	}

	private IBaseService<Department> myService;

	public IBaseService<Department> getMyService() {
		return myService;
	}

	public void setMyService(IBaseService<Department> myService) {
		this.myService = myService;
	}

	public DepartmentAction() {
		dataMap = new HashMap<String, Object>();
	}

	public String getEntity() {
		try {
			Department entity = myService.findById(department.getDepartmentId());
			BeanUtils.copyProperties(entity, department);
		} catch (Exception re) {

		}
		return SUCCESS;
	}
	
	public String getForm() {
		String companyId = ServletActionContext.getRequest().getParameter("cid");
		List<Department> dlist = myService.findList("companyId", Integer.parseInt(companyId));
		try {
			Department entity = myService.findById(department.getDepartmentId());
			BeanUtils.copyProperties(entity, department);
			dlist.remove(entity);
		} catch (Exception re) {

		}
		ActionContext.getContext().put("dlist", dlist);
		return SUCCESS;
	}

	public String getGridData() {
		dataMap.clear();
		try {
			int page = start == 0 ? 1 : start / length + 1;
			int size = length < 0 ? Integer.MAX_VALUE : length;

			String whereStr = "";

			String companyId = ServletActionContext.getRequest().getParameter("cid");

			if (!companyId.equals("")) {
				whereStr += " companyId = " + companyId;
			}

			Pager<Department> list = myService.findPagerList(page, size, whereStr, "");
			List<Department> a = list.getList();
			dataMap.put("draw", draw);
			dataMap.put("recordsTotal", list.getTotal());
			dataMap.put("recordsFiltered", list.getTotal());

			dataMap.put("data", a);
		} catch (Exception re) {
			dataMap.put("data", re.getMessage());
		}
		return SUCCESS;
	}

	public String getAllData() {
		try {
			String companyId = ServletActionContext.getRequest().getParameter("cid");
			List<Department> list = myService.findList("companyId",	Integer.parseInt(companyId));
			dataList = list;
		} catch (Exception re) {

		}
		return SUCCESS;
	}

	public String addEntity() {
		try {
			Department entity = new Department();
			BeanUtils.copyProperties(department, entity);
			myService.save(entity);
		} catch (Exception re) {
			return ERROR;
		}
		return SUCCESS;
	}

	public String editEntity() {
		dataMap.clear();
		try {
			Department entity = myService.findById(department.getDepartmentId());
			entity.setDepartmentName(department.getDepartmentName());
			entity.setParentDepartment(department.getParentDepartment());
			myService.update(entity);
			dataMap.put("data", true);
		} catch (Exception re) {
			return ERROR;
		}
		return SUCCESS;
	}

	public String delEntity() {
		dataMap.clear();
		try {
			Department entity = myService.findById(department.getDepartmentId());
			if(myService.delete(entity)){
				dataMap.put("valid", true);
			} else {
				dataMap.put("valid", false);
			}
		} catch (Exception re) {
			dataMap.put("valid", false);
		}
		return SUCCESS;
	}
}